package com.hbt.user_consumer.service;

import com.hbt.api.entity.CommonResult;
import com.hbt.api.entity.User;
import feign.codec.Encoder;
import feign.form.spring.SpringFormEncoder;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.cloud.openfeign.support.SpringEncoder;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@FeignClient(value = "user-provider", configuration = UserServiceFeign.MultipartSupportConfig.class)
public interface UserServiceFeign {
    String pre = "user_provider/";

    @RequestMapping(value = pre+"dologin", method = RequestMethod.POST)
    public CommonResult dologin(User user);

    @PostMapping(value = pre+"getUserByToken")
    public CommonResult getUserByToken(String token);

    @RequestMapping(method = RequestMethod.POST,value = pre+"uploadAvatar",produces = {MediaType.APPLICATION_JSON_UTF8_VALUE},consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public CommonResult uploadAvatar(MultipartFile file, @RequestParam("token")String token);

    class MultipartSupportConfig {
        @Autowired
        private ObjectFactory<HttpMessageConverters> messageConverters;

        @Bean
        public Encoder feignFormEncoder() {
            return new SpringFormEncoder(new SpringEncoder(messageConverters));
//            return new SpringFormEncoder();
        }
    }


}
